/*
 * Copyright (c) 2025 彭友聪
 * nativePC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 * Author: 彭友聪
 * email：2923616405@qq.com
 * date: 2025/10/8 09:57
 * file: ${FILE_NAME}
 * product: DevEco Studio
 * LICENSE: MulanPSL-2.0
 * */
//
// Created on 2025/10/8.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef NATIVEPC_ARKUITEXTNODE_H
#define NATIVEPC_ARKUITEXTNODE_H
#include "ArkUINode.h"
#include "NativeEntry.h"
#include <hilog/log.h>
#include <string>

namespace NativeModule
{
    
// 布局完成的回调方法
    void OnLayoutCompleted(void *userData)
    {
        showUITextCallback("监听布局事件", "布局完成回调");
        OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "the text_node is layout completed");
    }
// 绘制送显完成的回调方法
    void OnDrawCompleted(void *userData)
    {
        showUITextCallback("监听绘制事件", "绘制送显完成");
        OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "the text_node is draw completed");
    }
    class ArkUITextNode : public ArkUINode
    {
        public:
            ArkUITextNode()
                : ArkUINode((NativeModuleInstance::GetInstance()->GetNativeNodeAPI())->createNode(ARKUI_NODE_TEXT))
            {
            }
            // 文本属性NDK接口封装。
            void SetFontSize(float fontSize)
            {
                assert(handle_);
                ArkUI_NumberValue value[] = {{.f32 = fontSize}};
                ArkUI_AttributeItem item = {value, 1};
                nativeModule_->setAttribute(handle_, NODE_FONT_SIZE, &item);
            }
            void SetFontColor(uint32_t color)
            {
                assert(handle_);
                ArkUI_NumberValue value[] = {{.u32 = color}};
                ArkUI_AttributeItem item = {value, 1};
                nativeModule_->setAttribute(handle_, NODE_FONT_COLOR, &item);
            }
            void SetTextContent(const std::string &content)
            {
                assert(handle_);
                ArkUI_AttributeItem item = {nullptr, 0, content.c_str()};
                nativeModule_->setAttribute(handle_, NODE_TEXT_CONTENT, &item);
            }
            void SetTextAlign(ArkUI_TextAlignment align)
            {
                assert(handle_);
                ArkUI_NumberValue value[] = {{.i32 = align}};
                ArkUI_AttributeItem item = {value, 1};
                nativeModule_->setAttribute(handle_, NODE_TEXT_ALIGN, &item);
            }
            void SetLayoutCallBack(int32_t nodeId)
            {
                assert(handle_);
                OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "set layout callback");
                // 注册布局完成的回调方法
                OH_ArkUI_RegisterLayoutCallbackOnNodeHandle(handle_, this, OnLayoutCompleted);
            }
            void ResetLayoutCallBack()
            {
                assert(handle_);
                OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "reset layout callback");
                // 取消注册布局完成的回调方法
                OH_ArkUI_UnregisterLayoutCallbackOnNodeHandle(handle_);
            }
            void SetDrawCallBack(int32_t nodeId)
            {
                assert(handle_);
                OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "set draw callback");
                // 注册绘制送显完成的回调方法
                OH_ArkUI_RegisterDrawCallbackOnNodeHandle(handle_, this, OnDrawCompleted);
            }
            void ResetDrawCallBack()
            {
                assert(handle_);
                OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "reset draw callback");
                // 取消注册绘制送显完成的回调方法
                OH_ArkUI_UnregisterDrawCallbackOnNodeHandle(handle_);
            }
            void SetInspectorId(std::string inspectorId)
            {
                ArkUI_AttributeItem item = {nullptr, 0, inspectorId.c_str()};
                nativeModule_->setAttribute(handle_, NODE_ID, &item);
            }
            void AnimatorToString(int duration = 0, int delay = 0, int iterations = 0,
                                  ArkUI_AnimationFillMode fill = ARKUI_ANIMATION_FILL_MODE_FORWARDS,
                                  ArkUI_AnimationDirection direction = ARKUI_ANIMATION_DIRECTION_REVERSE,
                                  float begin = 0.0, float end = 0.0,
                                  const ArkUI_ExpectedFrameRateRange &expectedFrameRateRange = {0, 0, 0},
                                  float keyframeTime = 0.0, float keyframeValue = 0.0)
            {
                SetTextContent("Animator{ dur:" + std::to_string(duration) + ";  delay:" + std::to_string(delay) +
                               ";  iterations:" + std::to_string(iterations) + ";  fill:" + std::to_string(fill) +
                               ";  direction:" + std::to_string(direction) + ";  begin:" + std::to_string(begin) +
                               ";  end:" + std::to_string(end) +
                               ";  expectedFrameRateRange:[ max:" + std::to_string(expectedFrameRateRange.max) +
                               ", min:" + std::to_string(expectedFrameRateRange.min) +
                               ", expected:" + std::to_string(expectedFrameRateRange.expected) + "]" +
                               ";  keyframeTime:" + std::to_string(keyframeTime) +
                               ";  keyframeValue:" + std::to_string(keyframeValue) + "}");
            }

            void AnimatetoToString(int duration = 0, float tempo = 0, int curve = 0, int delay = 0, int iteration = 0,
                                   ArkUI_AnimationPlayMode playmode = ARKUI_ANIMATION_PLAY_MODE_REVERSE,
                                   const ArkUI_ExpectedFrameRateRange &expectedFrameRateRange = {0, 0, 0})
            {
                SetTextContent("animateto{ dur:" + std::to_string(duration) + ";  tempo:" + std::to_string(tempo) +
                               ";  curve:" + std::to_string(static_cast<int>(curve)) +
                               ";  delay:" + std::to_string(delay) + ";  iteration:" + std::to_string(iteration) +
                               ";  playMode:" + std::to_string(static_cast<int>(playmode)) +
                               ";  expectedFrameRateRange:[ max:" + std::to_string(expectedFrameRateRange.max) +
                               ", min:" + std::to_string(expectedFrameRateRange.min) +
                               ", expected:" + std::to_string(expectedFrameRateRange.expected) + "]}");
            }

            void KeyframeAnimatetoToString(int durationIndex1 = 0, int durationIndex2 = 0, int delay = 0,
                                           int iteration = 0,
                                           const ArkUI_ExpectedFrameRateRange &expectedFrameRateRange = {0, 0, 0})
            {
                SetTextContent("KeyframeAnimateto{ dur index:" + std::to_string(durationIndex1) +
                               ";  delay:" + std::to_string(delay) + ";  iteration:" + std::to_string(iteration) +
                               ";  expectedFrameRateRange:[ max:" + std::to_string(expectedFrameRateRange.max) +
                               ", min:" + std::to_string(expectedFrameRateRange.min) +
                               ", expected:" + std::to_string(expectedFrameRateRange.expected) + "]}");
            }
    };
} // namespace NativeModule

#endif // NATIVEPC_ARKUITEXTNODE_H
